package org.dtrd.modules.doctor.entity.bean;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.dtrd.config.system.Constant;
import org.dtrd.modules.doctor.entity.po.DtrdEntRdDoctorStatisticInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author qr
 * @date 2022/4/14 16:52
 */
@ApiModel("医生首页信息")
@Data
public class DoctorIndexInfo {

    @ApiModelProperty("患者总数")
    private Integer patientCount;
    @ApiModelProperty("患者总数差")
    private Integer patientCountDiff = 0;

    /**
     * 逆糖管理:
     */
    private Integer patientReverseCount = 0;
    /**
     * 控糖管理:
     */
    private Integer patientControlCount = 0;
    /**
     * 御糖管理:
     */
    private Integer patientImperialCount = 0;

    @ApiModelProperty("糖前期总数")
    private Integer preDiabetesCount;
    @ApiModelProperty("糖前期总数差")
    private Integer preDiabetesCountDiff = 0;
    @ApiModelProperty("2型糖尿病总数")
    private Integer diabetes2Count;
    @ApiModelProperty("2型糖尿病差")
    private Integer diabetes2CountDiff = 0;
    @ApiModelProperty("服务患者总数")
    private Integer servingPatientCount;
    @ApiModelProperty("服务患者总数差")
    private Integer servingPatientCountDiff = 0;
    @ApiModelProperty("联网血糖仪总数")
    private Integer gluDeviceCount;
    @ApiModelProperty("联网血糖仪差")
    private Integer gluDeviceCountDiff = 0;
    @ApiModelProperty("测量血糖总数")
    private Integer sugarCount;
    @ApiModelProperty("测量血糖总数差")
    private Integer sugarCountDiff = 0;

    @ApiModelProperty("糖尿病类型")
    private List<PieChartItem> diabetesType;

    @ApiModelProperty("用药类型")
    private List<PieChartItem> drugType;

    @ApiModelProperty("性别")
    private List<PieChartItem> sex;

    @ApiModelProperty("年龄")
    private List<PieChartItem> age;

    @ApiModelProperty("血糖")
    private List<PieChartItem> sugarAge;

    @ApiModelProperty("打卡饼装图")
    private List<paneItem> panes;


    @ApiModelProperty("打卡柱状图")
    private List<BarChartItem> barChartData;

    /**
     * 根据前端给的定
     */
    @ApiModel("饼状图项")
    @Getter
    @Setter
    @AllArgsConstructor
    public static class paneItem {

        private Integer dataId;
        private String groupName;
        private List<PieChartItem> data;
    }

    /**
     * 计算差值
     */
    public DoctorIndexInfo countDiff(DtrdEntRdDoctorStatisticInfo first, DtrdEntRdDoctorStatisticInfo second) {
        this.patientCountDiff = first.getPatientCount() - second.getPatientCount();
        this.diabetes2CountDiff = first.getPatientDiabetesType2() - second.getPatientDiabetesType2();
        this.preDiabetesCountDiff = first.getPatientPrediabetes() - second.getPatientPrediabetes();
        this.sugarCountDiff = first.getPatientSugarCount() - second.getPatientSugarCount();
        return this;
    }

    @SuppressWarnings("unchecked")
    public DoctorIndexInfo parseFromPO(DtrdEntRdDoctorStatisticInfo po) {
        //region 患者总数
        this.patientCount = po.getPatientCount();
        //endregion
        //region 糖尿病前期
        this.preDiabetesCount = po.getPatientPrediabetes();
        //endregion
        //region 2型糖尿病
        this.diabetes2Count = po.getPatientDiabetesType2();
        //endregion
        //region 测量血糖总数
        this.sugarCount = po.getPatientSugarCount();
        //endregion
        //region 性别
        List<PieChartItem> sexChart = new ArrayList<>(2);
        sexChart.add(new PieChartItem(Constant.Sex.MAN.getName(), po.getPatientSexMale()));
        sexChart.add(new PieChartItem(Constant.Sex.WOMAN.getName(), po.getPatientSexFemale()));
        this.sex = sexChart;
        //endregion
        //region 糖尿病类型
        List<PieChartItem> diabetesTypeChart = new ArrayList<>(4);
        diabetesTypeChart.add(new PieChartItem(Constant.DiabetesType.DIABETES_TYPE_1.getName(), po.getPatientDiabetesType1()));
        diabetesTypeChart.add(new PieChartItem(Constant.DiabetesType.DIABETES_TYPE_2.getName(), po.getPatientDiabetesType2()));
        diabetesTypeChart.add(new PieChartItem(Constant.DiabetesType.DIABETES_TYPE_PREGNANCY.getName(), po.getPatientDiabetesTypeGestation()));
        diabetesTypeChart.add(new PieChartItem(Constant.DiabetesType.DIABETES_TYPE_OTHER.getName(), po.getPatientDiabetesTypeOther()));
        this.diabetesType = diabetesTypeChart;
        //endregion
        //region 年龄
        List<PieChartItem> ageChart = new ArrayList<>(5);
        ageChart.add(new PieChartItem("0-10岁", po.getPatientAge1()));
        ageChart.add(new PieChartItem("11-20岁", po.getPatientAge2()));
        ageChart.add(new PieChartItem("20-40岁", po.getPatientAge3()));
        ageChart.add(new PieChartItem("40-90岁", po.getPatientAge4()));
        ageChart.add(new PieChartItem(">60岁", po.getPatientAge5()));
        this.age = ageChart;
        //endregion
        //region 用药类型
        ArrayList<PieChartItem> drugTypes = new ArrayList<>(3);
        drugTypes.add(new PieChartItem(Constant.DrugType.NO.getName(), po.getPatientDrugType1()));
        drugTypes.add(new PieChartItem(Constant.DrugType.ORAL_DRUG.getName(), po.getPatientDrugType2()));
        drugTypes.add(new PieChartItem(Constant.DrugType.INSULIN.getName(), po.getPatientDrugType3()));
        this.drugType = drugTypes;
        //endregion
        //region 糖龄
        List<PieChartItem> sugarAgeChart = new ArrayList<>(4);
        sugarAgeChart.add(new PieChartItem("1年以下", po.getPatientSugarAge1()));
        sugarAgeChart.add(new PieChartItem("1-5年", po.getPatientSugarAge2()));
        sugarAgeChart.add(new PieChartItem("5-10年", po.getPatientSugarAge3()));
        sugarAgeChart.add(new PieChartItem("10年以上", po.getPatientSugarAge4()));
        this.sugarAge = sugarAgeChart;
        //endregion
        //region 统计饼状图
        List<paneItem> paneItems = new ArrayList<>(11);
        String sugar = po.getPatientSigninSugar();
        if (StrUtil.isNotBlank(sugar)) {
            Map<String, Integer> sugarMap = JSONUtil.toBean(sugar, Map.class);
            List<PieChartItem> chartItems = map2PieCharItem(sugarMap);
            paneItems.add(new paneItem(1, "血糖", chartItems));
        }
        String diet = po.getPatientSigninDiet();
        if (StrUtil.isNotBlank(diet)) {
            Map<String, Integer> dietMap = JSONUtil.toBean(diet, Map.class);
            List<PieChartItem> chartItems = map2PieCharItem(dietMap);
            paneItems.add(new paneItem(2, "饮食", chartItems));
        }
        String sport = po.getPatientSigninSport();
        if (StrUtil.isNotBlank(sport)) {
            Map<String, Integer> sportMap = JSONUtil.toBean(sport, Map.class);
            List<PieChartItem> chartItems = map2PieCharItem(sportMap);
            paneItems.add(new paneItem(3, "运动", chartItems));
        }
        String medical = po.getPatientSigninMedical();
        if (StrUtil.isNotBlank(medical)) {
            Map<String, Integer> medicalMap = JSONUtil.toBean(medical, Map.class);
            List<PieChartItem> chartItems = map2PieCharItem(medicalMap);
            paneItems.add(new paneItem(4, "用药", chartItems));
        }
        String insulin = po.getPatientSigninInsulin();
        if (StrUtil.isNotBlank(insulin)) {
            Map<String, Integer> insulinMap = JSONUtil.toBean(insulin, Map.class);
            List<PieChartItem> chartItems = map2PieCharItem(insulinMap);
            paneItems.add(new paneItem(5, "胰岛素", chartItems));
        }
        String bloodPressure = po.getPatientSigninPressure();
        if (StrUtil.isNotBlank(bloodPressure)) {
            Map<String, Integer> bloodPressureMap = JSONUtil.toBean(bloodPressure, Map.class);
            List<PieChartItem> chartItems = map2PieCharItem(bloodPressureMap);
            paneItems.add(new paneItem(6, "血压", chartItems));
        }
        String ketone = po.getPatientSigninKetone();
        if (StrUtil.isNotBlank(ketone)) {
            Map<String, Integer> ketoneMap = JSONUtil.toBean(ketone, Map.class);
            List<PieChartItem> chartItems = map2PieCharItem(ketoneMap);
            paneItems.add(new paneItem(7, "尿酮", chartItems));
        }
        String waist = po.getPatientSigninWaist();
        if (StrUtil.isNotBlank(waist)) {
            Map<String, Integer> waistMap = JSONUtil.toBean(waist, Map.class);
            List<PieChartItem> chartItems = map2PieCharItem(waistMap);
            paneItems.add(new paneItem(8, "腰围", chartItems));
        }
        String weight = po.getPatientSigninWeight();
        if (StrUtil.isNotBlank(weight)) {
            Map<String, Integer> weightMap = JSONUtil.toBean(weight, Map.class);
            List<PieChartItem> chartItems = map2PieCharItem(weightMap);
            paneItems.add(new paneItem(9, "体重", chartItems));
        }
        String sleep = po.getPatientSigninSleep();
        if (StrUtil.isNotBlank(sleep)) {
            Map<String, Integer> sleepMap = JSONUtil.toBean(sleep, Map.class);
            List<PieChartItem> chartItems = map2PieCharItem(sleepMap);
            paneItems.add(new paneItem(10, "睡眠", chartItems));
        }
        String nutrient = po.getPatientSigninNutrient();
        if (StrUtil.isNotBlank(nutrient)) {
            Map<String, Integer> nutrientMap = JSONUtil.toBean(nutrient, Map.class);
            List<PieChartItem> chartItems = map2PieCharItem(nutrientMap);
            paneItems.add(new paneItem(11, "营养素", chartItems));
        }
        this.panes = paneItems;
        //endregion
        //region 统计柱状图
        String chart = po.getPatientSigninBarChart();
        if (StrUtil.isNotBlank(chart)) {
            Map<String, Integer> map = JSONUtil.toBean(chart, Map.class);
            // 转换下类型
            HashMap<Integer, Integer> typeToRate = new HashMap<>();
            for (Map.Entry<String, Integer> entry : map.entrySet()) {
                Integer type = Integer.valueOf(entry.getKey());
                Integer rate = entry.getValue();
                typeToRate.put(type, rate);
            }
            // 设置柱状图数据
            List<BarChartItem> barchart = new ArrayList<>(map.size());
            Integer sugarRate = typeToRate.get(Constant.RecordType.SUGAR.getType());
            if (sugarRate != null) {
                barchart.add(new BarChartItem(Constant.RecordType.SUGAR.getName(), sugarRate));
            }
            Integer dietRate = typeToRate.get(Constant.RecordType.DIET.getType());
            if (dietRate != null) {
                barchart.add(new BarChartItem(Constant.RecordType.DIET.getName(), dietRate));
            }
            Integer sportRate = typeToRate.get(Constant.RecordType.SPORT.getType());
            if (sportRate != null) {
                barchart.add(new BarChartItem(Constant.RecordType.SPORT.getName(), sportRate));
            }
            Integer medicalRate = typeToRate.get(Constant.RecordType.MEDICAL.getType());
            if (medicalRate != null) {
                barchart.add(new BarChartItem(Constant.RecordType.MEDICAL.getName(), medicalRate));
            }
            Integer insulinRate = typeToRate.get(Constant.RecordType.INSULIN.getType());
            if (insulinRate != null) {
                barchart.add(new BarChartItem(Constant.RecordType.INSULIN.getName(), insulinRate));
            }
            Integer bloodRate = typeToRate.get(Constant.RecordType.BLOOD_PRESSURE.getType());
            if (bloodRate != null) {
                barchart.add(new BarChartItem(Constant.RecordType.BLOOD_PRESSURE.getName(), bloodRate));
            }
            Integer ketoneRate = typeToRate.get(Constant.RecordType.KETONE.getType());
            if (ketoneRate != null) {
                barchart.add(new BarChartItem(Constant.RecordType.KETONE.getName(), ketoneRate));
            }
            Integer weightRate = typeToRate.get(Constant.RecordType.WEIGHT.getType());
            if (weightRate != null) {
                barchart.add(new BarChartItem(Constant.RecordType.WEIGHT.getName(), weightRate));
            }
            Integer sleepRate = typeToRate.get(Constant.RecordType.SLEEP.getType());
            if (sleepRate != null) {
                barchart.add(new BarChartItem(Constant.RecordType.SLEEP.getName(), sleepRate));
            }
            Integer nutrientRate = typeToRate.get(Constant.RecordType.NUTRIENT.getType());
            if (sleepRate != null) {
                barchart.add(new BarChartItem(Constant.RecordType.NUTRIENT.getName(), nutrientRate));
            }

            this.barChartData = barchart;
        }
        //endregion
        return this;
    }

    private List<PieChartItem> map2PieCharItem(Map<String, Integer> map) {
        List<PieChartItem> list = new ArrayList<>(map.size());
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            String key = entry.getKey();
            String name = Constant.SigninPlanPercentage.valueOf(Integer.valueOf(key)).getName();
            Integer value = entry.getValue();
            list.add(new PieChartItem(name, value));
        }
        return list;
    }

}
